package cn.seven._conf;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.jfinal.kit.PropKit;

import cn.seven._conf._fastjson.CustomFastJsonSerializer;
import cn.seven._conf._interceptor.ParamInterceptor;
import cn.seven._conf._interceptor.TestInterceptor;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;
import java.util.List;

/**
 * create by 张天笑, 2017/8/29 0:38
 */
@EnableAutoConfiguration
@Configuration
public class WebAppConfigurer extends WebMvcConfigurerAdapter {


    /*
   * 配置拦截器,指定拦截路径
   * */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new ParamInterceptor()).addPathPatterns("/**");
        //registry.addInterceptor(new GlobalInterceptor()).addPathPatterns("/**");
    }


    /*
    * 配置静态资源过滤
    * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    /*
    * 文件上传配置
    * 临时目录中的文件名由springmvc自己处理， 不用担心重名的问题。
    * */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();

        // factory.setMaxFileSize("1KB"); //单位KB,MB, 限制单个input=file控件的文件大小,

        /// 设置总上传数据总大小, 这里设置表单大小10kb, 相当于禁用文件上传
        factory.setMaxRequestSize("1024KB");

        //factory.setFileSizeThreshold("100KB");//大于100kb的文件暂存临时目录

        factory.setLocation(PropKit.get("fileTmpDir"));
        return factory.createMultipartConfig();
    }

    /*
    * 替换Json转换器
    * */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        fastConverter.setFastJsonConfig(CustomFastJsonSerializer.config(new FastJsonConfig()));
        converters.add(fastConverter);
    }

    /*
    * 跨域设置
    * */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //.allowedHeaders("Access-Control-Allow-Credentials","Access-Control-Allow-Origins","Content-Type:","Set-Cookie","Vary")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }

}
